﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows;
using System.Text;
using System.Windows.Threading;
using ExamSharedClasses;

namespace ExamServer
{
    /// <summary>
    /// Interaktionslogik für "App.xaml"
    /// </summary>
    public partial class App : Application
    {
        // Programm
        public const string VERSION = "0.98";
        public const string SOURCE = "server";
        public static ServerMainWindow ServerMainWindow { get; set; }
        
        // Server
        public static ClientConnection ClientConnection { get; set; }
        public static ClientConnectionThreadList ClientConnectionThreadList { get; set; }
        public static DatabaseConnection DatabaseConnection { get; set; }
        public static bool IsLoginAllowed { get; set; }

        public static DispatcherTimer dispatcherTimer { get; set; }

        public static Queue<string> LogStringQueue { get; set; }

        /// <summary>
        /// (k) Ausgabe von Text, der für den User bestimmt ist
        /// </summary>
        /// <param name="messageObject">Alles, was etwas Sinnvolles bei ToString() zurückgibt</param>
        public static void Display(object messageObject)
        {
            Display(messageObject, null);
        }
        public static void Display(object messageObject, object secondMessageObject)
        {
            StringBuilder sb = new StringBuilder(DateTime.Now + ": ");

            if (messageObject != null) {
                if (messageObject is Exception)
                    sb.Append(((Exception)messageObject).Message);
                else
                    sb.Append(messageObject.ToString());
            }
            if (secondMessageObject != null) {
                if (secondMessageObject is Exception)
                    sb.Append('\n' + ((Exception)secondMessageObject).Message);
                else
                    sb.Append('\n' + secondMessageObject.ToString());
            }

            string text = sb.ToString();
            if ( !LogFilterMethod(text).Equals("") )
                App.LogStringQueue.Enqueue(text);                                        // Anhängen an Queue

            if ( text.Contains( "Datenbank" ) )
                Console.ForegroundColor = ConsoleColor.White;
            if ( text.Contains( "Datenbank" ) && text.Contains( "INSERT") )
                Console.ForegroundColor = ConsoleColor.Yellow;
            if ( text.Contains( "Datenbank" ) && text.Contains( "SELECT" ) )
                Console.ForegroundColor = ConsoleColor.Green;
            if ( text.Contains( "!" ) )
                Console.ForegroundColor = ConsoleColor.DarkYellow;
            if ( text.Contains( "Fehler" ) )
                Console.ForegroundColor = ConsoleColor.DarkRed;
            if ( text.Contains( "Datenbank" ) && text.Contains( "Fehler" ) )
                Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine( text );                                                // Ausgabe auf Konsole
            Console.ForegroundColor = ConsoleColor.Gray;

            Tools.AppendStringToFile(text, App.SOURCE + "_log.txt"); // Anhängen an Datei
        }

        /// <summary>
        /// für die listbox des server-fenster entstehen zu viele logausgaben, die man
        /// aber in datei und konsole haben möchte...
        /// </summary>
        public static string LogFilterMethod( string message ) {
            if ( message.Contains( "Fehler" ) )
                return message;

            else if ( message.Contains( "SELECT" ) || message.Contains( "INSERT" ) )
                return "";


            else
                return message;
        }
    }
}
